﻿using System;
using System.Collections.Generic;
using System.Windows;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;
using Telerik.Windows.Controls;
using Baosight.ZNH.ZMCL.UI.PopWindows;
using Baosight.ZNH.ZMCommon.ChildWindows;
using System.Collections.ObjectModel;
using System.Linq;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCL.UI
{
    /// <summary>
    /// 待处理三违页面
    /// </summary>
    /// Title:ZMCL61
    /// Service:ServiceZMCL61
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2011-06-24  高强   [创建]
    ///  
    public partial class ZMCL61 : EFViewCmes
    {
        public ZMCL61()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }

        /// <summary>
        /// 修改前设置属性
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.EFGridCheckedRows = this.grid.GetCheckedEIRows();
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
        }

        /// <summary>
        /// 下达三违通知
        /// 按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnHandleButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                if (!"未处理".Equals(row["fdStatus"].ToString()))
                {
                    this.OpenEFWindowAlert("三违记录已经下达通知，应该关闭三违记录！", true);
                    return;
                }
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup("通知下达", eiInfo, "三违通知-下达", "update", "handle", sender, e);
        }

        /// <summary>
        /// 罚款
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnPenaltyButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();
            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");

            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                if ("未处理".Equals(row["fdStatus"].ToString()))
                {
                    this.OpenEFWindowAlert("三违记录还未处理，不能罚款！", true);
                    return;
                }
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);
            base.OnUpdateButtonClickPopup("罚款", eiInfo, "三违处理—罚款", "insert", "penalty", sender, e);
        }

        /// <summary>
        /// “关闭”按钮执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnCloseButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("close", "关闭", sender, e);
        }

        /// <summary>
        /// 点击新增按钮，弹出新增页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnInsertButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            base.OnInsertButtonClickPopup(eiInfo, "三违记录-新增", "insert", null, sender, e);
        }

        /// <summary>
        /// 点击修改按钮，弹出修改页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            IList<EIRow> _list = this.grid.GetCheckedEIRows() == null ? new List<EIRow>() : this.grid.GetCheckedEIRows();

            EIInfo eiInfo = new EIInfo();
            EIBlock eIBlock = new EIBlock("result");
            EIRow row = new EIRow();
            if (_list.Count > 0)
            {
                row = BaseTools.GetEIRowByOldEIRow(this.EIDataContext.GetBlock("result").GetColumns(), _list[0]);
                string fdStatus = row["fdStatus"] == null ? "" : row["fdStatus"].ToString();
                if (!"未处理".Equals(fdStatus))
                {
                    this.OpenEFWindowAlert("只能修改未处理的三违记录！", true);
                    return;
                }
            }
            eIBlock.SetRow(0, row);
            eiInfo.AddBlock(eIBlock);

            base.OnUpdateButtonClickPopup(eiInfo, "三违记录-修改", "update", null, sender, e);
        }

        /// <summary>
        /// 点击作废按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OnInvalidButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnModifyButtonClick("invalid", "作废", sender, e);
        }

        /// <summary>
        /// 新增或修改之前的数据传输
        /// 构造新增页面内容、弹出页面
        /// </summary>
        /// <param name="eIInfo"></param>
        /// <param name="title"></param>
        public override void OpenPopup(EIInfo _eIInfo, string _title, string _dialogType, string _args)
        {
            if (null != _args && "handle".Equals(_args))
            {
                TernateHandle _handle = new TernateHandle(_dialogType);

                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.classify"));//添加三违分类block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.sourceClassify"));//添加三违来源block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.workNum"));//添加班次block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.department"));//添加三违单位block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.responsibilityPeople"));//添加检查人block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.roadWay"));//添加巷道block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.workFace"));//添加工作面block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.workNote"));//添加巡检工单编号block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.penaltyType"));//添加罚款分类block

                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.examine"));//五个到位分类
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.quality"));//质量标准化分类

                _handle.EIDataContext = new Platform.Core.UI.EIDataContext();
                _handle.EIDataContext.Update(_eIInfo);

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = _title == null ? "" : _title;//设置Title
                _popup.Content = _handle;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口
            }
            else if (null != _args && "penalty".Equals(_args))
            {
                EIInfo Info = new EIInfo();

                TernatePenalty _penalty = new TernatePenalty("insert");//构造一个新的修改页面
                Info.AddBlock(this.EIDataContext.GetBlock("cmes.cl.department"));
                Info.AddBlock(this.EIDataContext.GetBlock("cmes.cl.penaltyType"));//罚款类型

                _penalty.EIDataContext = new Platform.Core.UI.EIDataContext();
                _penalty.EIDataContext.Update(Info);

                Collection<EIRow> penaltyType = this.EIDataContext.GetBlock("cmes.cl.penaltyType").GetRows();
                IEnumerable<EIRow> penaltyTypeResult = from c in penaltyType where c["display"].ToString().IndexOf("现金") >= 0 select c;
                if (penaltyTypeResult.Count() > 0)
                {
                    _penalty.fdClassify.Value = penaltyTypeResult.First()["value"].ToString();
                }

                EIRow eIRow = _eIInfo.GetBlock("result").GetRow(0);
                _penalty.fdSendnoteDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                _penalty.fdTernateRecordId.Value = eIRow["fdKeyId"];//三违记录ID
                _penalty.fdSendnoteDepartment.Value = eIRow["fdManageDepartment"];//安全主管部门
                _penalty.fdDepartmentId.Value = eIRow["fdResponseDepartmentId"];//责任部门ID
                _penalty.fdDepartmentName.Value = eIRow["fdResponseDepartment"];//责任部门

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = _title == null ? "" : _title;//设置Title
                _popup.Content = _penalty;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口
            }
            else
            {
                TernateInsert _deregulation = new TernateInsert(_dialogType);//构造一个新增三违页面

                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.classify"));//添加三违分类block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.sourceClassify"));//添加三违来源block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.workNum"));//添加班次block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.department"));//添加三违单位block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.responsibilityPeople"));//添加检查人block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.roadWay"));//添加巷道block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.workFace"));//添加工作面block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.workNote"));//添加巡检工单编号block
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.penaltyType"));//添加罚款分类block

                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.examine"));//五个到位分类
                _eIInfo.AddBlock(this.EIDataContext.GetBlock("cmes.cl.quality"));//质量标准化分类

                _deregulation.EIDataContext = new Platform.Core.UI.EIDataContext();
                _deregulation.EIDataContext.Update(_eIInfo);

                _deregulation.fdManageDepartmentId.Value = this.EIDataContext.GetAttribute("departmentID") == null ? "" : this.EIDataContext.GetAttribute("departmentID").ToString();//默认主管部门为当前登录人所在部门
                _deregulation.fdManageDepartment.Value = this.EIDataContext.GetAttribute("departmentName") == null ? "" : this.EIDataContext.GetAttribute("departmentName").ToString();//默认主管部门为当前登录人所在部门

                CmesWindows _popup = new CmesWindows();//构造一个弹出页面
                _popup.ResizeMode = ResizeMode.NoResize;//设置不能修改窗口大小
                _popup.Header = _title == null ? "" : _title;//设置Title
                _popup.Content = _deregulation;//让新增页面在弹出窗口显示
                _popup.Closed += new EventHandler<WindowClosedEventArgs>(OpenWindow_Closed);
                _popup.ShowDialog();//模态显示弹出窗口
            }
        }

        /// <summary>
        /// 弹出窗口关闭时执行的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenWindow_Closed(object sender, WindowClosedEventArgs e)
        {
            //按钮生效
            base.TempButtonOpen(true);

            if (e.DialogResult == true)
            {
                //刷新Grid区域
                this.grid.Refresh();
            }
        }

        /// <summary>
        /// 显示从表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (null != this.grid.SelectedItem)
            {
                EIRow _row = (this.grid.SelectedItem as UIBaseEntity).GetEIRow();
                string fdStatus = _row["fdStatus"] == null ? "" : _row["fdStatus"].ToString();

                //是否未处理
                if (!"未处理".Equals(fdStatus))
                {
                    Dictionary<string, string> args = new Dictionary<string, string>();
                    args.Add("fdTernateRecordId", _row["fdKeyId"].ToString());
                    this.deregulationDetails.LoadView<EFCustomViewContainerPanel>("ZMCL63", args);//加载从表页面

                    Dictionary<string, string> args1 = new Dictionary<string, string>();
                    args1.Add("fdPenaltyIds", _row["fdPenaltyIds"] == null ? "" : _row["fdPenaltyIds"].ToString());
                    this.deregulationPenalty.LoadView<EFCustomViewContainerPanel>("ZMCL75", args1);//加载从表页面
                }
                else
                {
                    this.deregulationDetails.Content = null;
                    this.deregulationPenalty.Content = null;
                }
            }
            else
            {
                this.deregulationDetails.Content = null;
                this.deregulationPenalty.Content = null;
            }
        }

    }
}
